<?php
/*
	Copyright 2010 Ping Shin Ching (ping_sc@hotmail.com)

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
	
	Project Mugatiya: http://www.tidytutorials.com/p/mugatiya.html
 */

require_once 'adds.php';


/*
 * If true then bypass content filtering - faster response
 * Default add will be 'unclassified'
 */
define('STREAMLINE', false);



/*
 * Lossy compression of jpegs
 */
define("SMALLENJPEGS", false);

/*
 * Modify the post parameters and values
 */
function imodpostvalues($url, $host, $post){
	return $post;	
}


/*
 * Modify the response data
 */
include 'contentfilter.php';
function imodresponse(&$output){
	global $myhost;
	
	$divheight	= 57;
	$hideheight	= 23;
	$hidewidth = 50;
	$totaldivheight=($divheight+40);
	$external=0;
	$bgcolor="";

	if(STREAMLINE){
		$rating = "U";	
	}
	else{
		$rating=getContentRating($output);
	}
	
	$adpage=getMyAdd($rating);
	
	if($rating=="G")
		$ratingicon="<font class=\\\"prxftxt\\\" style=\\\"background-color:green\\\">&nbsp;G&nbsp;</font>";
	else if($rating=="M")
		$ratingicon="<font class=\\\"prxftxt\\\" style=\\\"background-color:blue\\\">&nbsp;M&nbsp;</font>";	
	else if($rating=="R")
		$ratingicon="<font class=\\\"prxftxt\\\" style=\\\"background-color:red\\\">&nbsp;R&nbsp;</font>";	
	else
		$ratingicon="<font class=\\\"prxftxt\\\" style=\\\"background-color:#cc9900\\\">&nbsp;U&nbsp;</font>";

	/*Header*/	
	$head  =	"<div id=\"add_place_dvx\" style=\"height:".$totaldivheight."px;overflow:hidden\"></div><div scrolling=\"no\" style=\"margin:0 0 0 0;z-index:1000;overflow:hidden;position:absolute;top:0px;left:0px;width:100%;\" id=\"add_link_dvx\"></div>".
				"<script>".
				"if(top.location==self.location){".
				"var x=document.getElementById('add_link_dvx');x.style.height=\"".$totaldivheight."px\";x.visibility=\"visible\";".
				"x.innerHTML=\"<table class=\\\"prxtbl\\\" style=\\\"border-width:0;border-collapse:collapse;width:100%;".($bgcolor==""?"":"background-color:#$bgcolor")."\\\">".
				"<tr><td><table class=\\\"prxtbl\\\" style=\\\"".($bgcolor==""?"":"background-color:#$bgcolor")."\\\"><tr><td><span class=\\\"prxflnk\\\" id=\\\"add_show_dvx\\\"></span></td><td>&nbsp</td><td>&nbsp;&nbsp;</td><td><span class=\\\"prxftxt\\\">".getMyProxyTitle()."</span></td><td>&nbsp;</td><td>$ratingicon</td><td style=\\\"width:100%\\\"><span class=\\\"prxflnk\\\" id=\\\"add_hide_dvx\\\">&nbsp;&nbsp;<a class=\\\"prxflnk\\\" href=\\\"javascript:hidemeplease()\\\">Hide!</a></span></td><td></td></tr></table></td></tr>".
				"<tr><td><iframe id=\\\"add_link_ifrmx\\\" frameborder=\\\"0\\\" scrolling=\\\"no\\\" class=\\\"prxifrm\\\" src=\\\"$adpage\\\"></iframe></td></tr></table>\";".
				"document.getElementById('add_link_ifrmx').style.height=\"".$divheight."px\";".
				"}else{".
				"document.getElementById('add_place_dvx').style.height=\"0px\";}".
				"function hidemeplease(){var x=document.getElementById('add_link_dvx');x.style.height=\"".$hideheight."px\";x.style.width=\"".$hidewidth."px\";var y=document.getElementById('add_place_dvx');y.style.height=\"".$hideheight."px\";y.style.width=\"".$hidewidth."px\"; document.getElementById('add_hide_dvx').innerHTML=\"\";document.getElementById('add_show_dvx').innerHTML=\"<left><a class=\\\"prxflnk\\\" href=\\\"javascript:showmeplease()\\\">Show!</a>&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/></left>\";  }".
				"function showmeplease(){var x=document.getElementById('add_link_dvx');x.style.height=\"".$totaldivheight."px\";x.style.width=\"100%\";var y=document.getElementById('add_place_dvx');y.style.height=\"".$totaldivheight."px\";y.style.width=\"100%\";document.getElementById('add_show_dvx').innerHTML=\"\";document.getElementById('add_hide_dvx').innerHTML=\"<a class=\\\"prxflnk\\\" href=\\\"javascript:hidemeplease()\\\">Hide!</a>\";}".
				"</script>".
				"<noscript>".
				"<div style=\"position:absolute;top:0px;left:0px;width:100%\"><table class=\"prxtbl\" style=\"border-width:0;border-collapse:collapse;width:100%;".($bgcolor==""?"":"background-color:#$bgcolor")."\">".
				"<tr><td><table class=\"prxtbl\" style=\"".($bgcolor==""?"":"background-color:#$bgcolor")."\"><tr><td><span class=\"prxflnk\" id=\"add_show_dvx\"></span></td><td>&nbsp</td><td>&nbsp;&nbsp;</td><td><span class=\"prxftxt\">".getMyProxyTitle()."</span></td><td>&nbsp;</td><td>$ratingicon</td><td style=\"width:100%\">&nbsp;</td><td><span class=\"prxflnk\" id=\"add_hide_dvx\"></span></td><td></td></tr></table></td></tr>".
				"<tr><td><iframe id=\"add_link_ifrmx\" frameborder=\"0\" scrolling=\"no\" class=\\\"prxifrm\\\"  style=\"height:".$divheight."px;width:100%\" src=\"$adpage\"></iframe></td></tr></table></div>".
				"</noscript>";
	
	$output=preg_replace("/<\s*\/\s*head\s*>/ims", "<link rel=\"stylesheet\" type=\"text/css\" href=\"$myhost/spr/menu/menu.css\" /></head>", $output);
	
	if(preg_match(SRC_REGX_BODYTAG, $output, $matches)){
		$pos=strpos($output, $matches[0]);
		if($pos === FALSE)
			return $output;
		else	
			return substr_replace($output, $matches[0].$head, $pos, strlen($matches[0]));
	}		
	else
		return $output;
}

/*
 * Modify the response data
 */
function imodmodifybinary($contenttype){
	if(SMALLENJPEGS && stripos($contenttype, "image/jpeg")!==FALSE){
		return "LossifyJpegImage";
	}
	else	
		return false;
}

/*
 * Modify the response data
 */

function LossifyJpegImage(& $data){
    $size=strlen($data);
    if($size<2048){
        echo $data;
    }
    else if(($im = imagecreatefromstring($data))!==FALSE){
        $x = imagesx ($im);
        $y = imagesy ($im);
        $ratio = intval(($size*100)/($y*$x));
        $quality = intval(log($ratio)/0.043);
        imagejpeg($im, NULL, intval($quality/1.5));
        file_put_contents('php://stderr', "size $size, assumed quality $quality\n");
        imagedestroy($im);
    }else{
        echo $data;
    }
}

?>
